package com.ws.shop.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.LinkedList;
import java.util.List;

import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.ResultSetExtractor;
import org.springframework.stereotype.Repository;

import com.ws.shop.dao.PackagzDao;
import com.ws.shop.entity.Packagz;

@Repository(value="packagzDao")
public class PackagzDaoImpl extends BaseDaoImpl implements PackagzDao {
	
	final String findListAll="select id,amount,beat,pointb,pointc,path from jvf_packagz order by id desc";
	public List<Packagz> findListAll() {
		return this.query(findListAll, new ResultSetExtractor<List<Packagz>>(){
			public List<Packagz> extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				List<Packagz> list = new LinkedList<Packagz>();
				Packagz p = null;
				while(rs.next()){
					p = new Packagz();
					p.setId(rs.getInt(1));
					p.setAmount(rs.getInt(2));
					p.setBeat(rs.getInt(3));
					p.setPointb(rs.getInt(4));
					p.setPointc(rs.getInt(5));
					p.setPath(rs.getInt(6));
					list.add(p);
				}
				return list;
			}
		});
	}

	final String findPackagzById = "select amount,beat,pointb,pointc,path from jvf_packagz where id=?";
	public Packagz findPackagzById(final int id) {
		return this.query(findPackagzById, new ResultSetExtractor<Packagz>(){
			public Packagz extractData(ResultSet rs) throws SQLException,
					DataAccessException {
				if(!rs.next())
					return null;
				Packagz p = new Packagz();
				p.setId(id);
				p.setAmount(rs.getInt(1));
				p.setBeat(rs.getInt(2));
				p.setPointb(rs.getInt(3));
				p.setPointc(rs.getInt(4));
				p.setPath(rs.getInt(5));
				return p;
			}
		}, id);
	}

	final String save_sql = "insert into jvf_packagz(amount,beat,pointb,pointc,path) values(?,?,?,?,?)";
	final String update_sql = "update jvf_packagz set amount=?,beat=?,pointb=?,path=? where id=?";
	public void saveOrUpdate(Packagz packagz) {
		if(packagz.getId() == 0){
			this.update(save_sql, packagz.getAmount(),packagz.getBeat(),packagz.getPointb(),packagz.getPointc(),packagz.getPath());
		}else{
			this.update(update_sql,packagz.getAmount(),packagz.getBeat(),packagz.getPointb(),packagz.getPath(),packagz.getId());
		}
	}

}
